package cn.itcast.crm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.crm.dao.CustomerRepository;
import cn.itcast.crm.domain.Customer;
import cn.itcast.crm.service.CustomerService;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private CustomerRepository customerRepository;

	@Override
	public List<Customer> findOrphanCustomers() {
		return customerRepository.findByFixedAreaIdIsNull();
	}

	@Override
	public List<Customer> findAssociatedCustomer(String fixedAreaId) {
		return customerRepository.findByFixedAreaId(fixedAreaId);
	}

	@Override
	public void associate(List<Integer> customerIds, String id, List<Integer> orphanIds) {
        //判断是否没有选中任何一个顾客
       /* if(customerIds.size()==0){
            customerRepository.unassociate(customerIds);
        }else if (orphanIds.size()==0) {
            customerRepository.associate(orphanIds, id);
        }else {*/
            customerRepository.associate(customerIds, id);
            customerRepository.unassociate(orphanIds);
        /**/
	}

	@Override
	public void register(Customer customer) {
		customerRepository.save(customer);
	}

	@Override
	public Customer findByPhoneNumber(String telephone) {
		return customerRepository.findByTelephone(telephone);
	}

	@Override
	public void updateType(String telephone) {
		customerRepository.updateType(telephone);
	}


}
